﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmEditRoom : BaseForm
    {
        public string ID = string.Empty;

        public FrmEditRoom()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void InitRoomType()
        {
            List<RoomTypeInfo> list = BLLFactory<RoomType>.Instance.GetAll();
            this.cmbRoomType.Items.Clear();
            foreach (RoomTypeInfo typeInfo in list)
            {
                CListItem item = new CListItem(typeInfo.Name, typeInfo.Name);
                this.cmbRoomType.Items.Add(item);
            }
        }

        private void FrmEditRoomType_Load(object sender, EventArgs e)
        {
            InitRoomType();
            InitDictItem();
            string chkNotClose = RegistryHelper.GetValue(Constants.IS_ROOMS_EDIT_OPEN);
            if (!string.IsNullOrEmpty(chkNotClose))
            {
                this.chkNotClose.Checked = (chkNotClose == "0") ? false : true;
            }

            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "编辑 " + this.Text;
                RoomInfo info = BLLFactory<Room>.Instance.FindByID(ID);
                if (info != null)
                {
                    this.cmbRoomType.Text = info.RoomType;
                    this.cmbBuildNum.Text = info.BuildNum;
                    this.cmbFloor.Text = info.Floor;
                    this.txtLockNumber.Text = info.LockNumber;
                    this.txtRoomNO.Text = info.RoomNo;
                    this.txtTelephone.Text = info.RoomTelephone;
                }
                //this.btnOK.Enabled = Portal.gc.HasFunction("Product/Modify");
            }
            else
            {
                this.Text = "新建 " + this.Text;
                //this.btnOK.Enabled = Portal.gc.HasFunction("Product/Add");
            }
            this.cmbRoomType.Focus();
        }

        private void InitDictItem()
        {


            this.cmbBuildNum.Items.Clear();
            this.cmbBuildNum.Items.AddRange(DictItemUtil.GetRoomBuildNumList());

            this.cmbFloor.Items.Clear();
            this.cmbFloor.Items.AddRange(DictItemUtil.GetRoomFloorType());
        }

        private void SetInfo(RoomInfo info)
        {
            info.LockNumber = this.txtLockNumber.Text;
            //info.Region = this.txtArea.Text;
            info.RoomNo = this.txtRoomNO.Text;
            info.RoomTelephone = this.txtTelephone.Text;
            info.RoomType = this.cmbRoomType.Text;
            info.BuildNum = this.cmbBuildNum.Text;
            info.Floor = this.cmbFloor.Text;
        }
        private void ClearInput()
        {
            this.ID = "";
            //this.txtArea.Clear();
            this.txtLockNumber.Clear();
            this.txtRoomNO.Clear();
            this.txtTelephone.Clear();

            this.Text = this.Text.Replace("编辑", "新建");
            this.cmbRoomType.Focus();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.cmbRoomType.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请选择房间类型");
                this.cmbRoomType.Focus();
                return;
            }
            if (this.txtRoomNO.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请输入房间编号");
                this.txtRoomNO.Focus();
                return;
            }

            if (this.cmbBuildNum.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请选择楼栋");
                this.cmbBuildNum.Focus();
                return;
            }
            if (this.cmbFloor.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请选择楼层");
                this.cmbFloor.Focus();
                return;
            }

            if (!string.IsNullOrEmpty(ID))
            {
                RoomInfo info = BLLFactory<Room>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<Room>.Instance.Update(info, info.ID.ToString());
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            if (this.chkNotClose.Checked)
                            {
                                ClearInput();
                            }
                            else
                            {
                                this.DialogResult = DialogResult.OK;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                RoomInfo info = new RoomInfo();
                SetInfo(info);
                info.Status = Entity.RoomStatus.空净房;

                try
                {
                    bool succeed = BLLFactory<Room>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        if (this.chkNotClose.Checked)
                        {
                            ClearInput();
                        }
                        else
                        {
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }

        private void chkNotClose_CheckedChanged(object sender, EventArgs e)
        {
            //保存注册表，记录下来
            RegistryHelper.SaveValue(Constants.IS_ROOMS_EDIT_OPEN, this.chkNotClose.Checked ? "1" : "0");

        }
    }
}
